//
//  IGTimeSwitchVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/11/17.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTimeSwitchVC.h"
#import "IGTimeSwitchCell.h"
#import "IGTimeSwitchApi.h"
#import "IGPowerTimeApi.h"
#import "IGTimeEditVC.h"
@interface IGTimeSwitchVC ()<UITableViewDelegate, UITableViewDataSource>
{

}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) IGEquRemoteModel *model;
@property (nonatomic, strong) IGTimeSwitchApi *api;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation IGTimeSwitchVC

- (instancetype)initWithModel:(IGEquRemoteModel *)model
{
    self = [super init];
    if (self)
    {
        self.model = model;
    }
    return self;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    self.api = [IGTimeSwitchApi new];
//    self.title = @"定时开关机";
    self.title = NSLocalizedString(@"Timer Switch", nil);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)loadData
{
    self.api.macId = _model.macId;
    [self.api startWithDecodeClass:[IGTimeSwitchResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            IGTimeSwitchResp *resp = responseObject;
            IGTimeSwitchModel *model = resp.data;
            if (model.powerTimes && model.powerTimes.count > 0)
            {
                self.dataSource = [NSMutableArray arrayWithArray:model.powerTimes];
            }
            [self.tableView reloadData];
        }
    }];
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
        [_tableView registerNib:[UINib nibWithNibName:@"IGTimeSwitchCell" bundle:nil] forCellReuseIdentifier:@"IGTimeSwitchCell"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count ? : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IGTimeSwitch *model = self.dataSource[indexPath.row];
    IGTimeEditVC *vc = [[IGTimeEditVC alloc] initWithModel:model];
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGTimeSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IGTimeSwitchCell" forIndexPath:indexPath];
    IGTimeSwitch *data = self.dataSource[indexPath.row];
    
        NSMutableString *onTime = [NSMutableString stringWithString:data.onTime];
        [onTime insertString:@":" atIndex:2];
        NSMutableString *offTime = [NSMutableString stringWithString:data.offTime];
        [offTime insertString:@":" atIndex:2];
    //        cell.timeL.text = [NSString stringWithFormat:@"%@开 %@关", onTime, offTime];
        NSString *ONString = NSLocalizedString(@"ON", nil);
        NSString *OFFString = NSLocalizedString(@"OFF", nil);
        cell.timeL.text = [NSString stringWithFormat:@"%@ %@ %@ %@", onTime,ONString,offTime,OFFString];
    
    if ([data.type isEqualToString:@"0"])
    {
//        cell.typeL.text = @"每天";
        cell.typeL.text = NSLocalizedString(@"Daily", nil);

    }
    else if ([data.type isEqualToString:@"1"])
    {
//        cell.typeL.text = @"工作日";
        cell.typeL.text = NSLocalizedString(@"Working Day", nil);
    }
    else if ([data.type isEqualToString:@"2"])
    {
//        cell.typeL.text = @"周末";
        cell.typeL.text = NSLocalizedString(@"Weekend", nil);
    }
    cell.switchBtn.on = [data.switchs boolValue];
    cell.switchBlock = ^(UISwitch *switchBtn) {
        [self switchBntChange:data switchBtn:switchBtn];
    };
    return cell;
}

- (void)switchBntChange:(IGTimeSwitch *)data switchBtn:(UISwitch *)switchBtn
{
    IGPowerTimeApi *api = [IGPowerTimeApi new];
    api.macId = data.macId;
    api.groupStr = data.group;
    api.switchsArr = [data.switchs isEqualToString:@"0"] ? @"1" : @"0";
    api.onTimeStr = data.onTime;
    api.offTimeStr = data.offTime;
    api.typeStr = data.type;
    [api startWithDecodeClass:[IGPowerTimeResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            data.switchs = api.switchsArr;
        }
        else
        {
            switchBtn.on = !switchBtn.on;
        }
    }];
}



@end
